package com.score.handle;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.score.pojo.ResponseEntry;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint{
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setStatus(200);
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        PrintWriter out = httpServletResponse.getWriter();
        ResponseEntry responseEntry = new ResponseEntry(403,null,"尚未登录,请登录!");
        out.write(new ObjectMapper().writeValueAsString(responseEntry));
        out.flush();
        out.close();
    }
}